
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>GeoJSON Serializer &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="GeoDjango Management Commands" href="commands.html" />
    <link rel="prev" title="OGR Inspection" href="ogrinspect.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="ogrinspect.html" title="OGR Inspection">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="commands.html" title="GeoDjango Management Commands">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-gis-serializers">
            
  <div class="section" id="s-module-django.contrib.gis.serializers.geojson">
<span id="s-geojson-serializer"></span><span id="module-django.contrib.gis.serializers.geojson"></span><span id="geojson-serializer"></span><h1><code class="docutils literal notranslate"><span class="pre">GeoJSON</span></code> Serializer<a class="headerlink" href="#module-django.contrib.gis.serializers.geojson" title="永久链接至标题">¶</a></h1>
<p>GeoDjango provides a specific serializer for the <a class="reference external" href="https://geojson.org/">GeoJSON</a> format. See
<a class="reference internal" href="../../../topics/serialization.html"><span class="doc">Serializing Django objects</span></a> for more information on serialization.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">geojson</span></code> serializer is not meant for round-tripping data, as it has no
deserializer equivalent. For example, you cannot use <a class="reference internal" href="../../django-admin.html#django-admin-loaddata"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">loaddata</span></code></a> to
reload the output produced by this serializer. If you plan to reload the
outputted data, use the plain <a class="reference internal" href="../../../topics/serialization.html#serialization-formats-json"><span class="std std-ref">json serializer</span></a>
instead.</p>
<p>In addition to the options of the <code class="docutils literal notranslate"><span class="pre">json</span></code> serializer, the <code class="docutils literal notranslate"><span class="pre">geojson</span></code>
serializer accepts the following additional option when it is called by
<code class="docutils literal notranslate"><span class="pre">serializers.serialize()</span></code>:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">geometry_field</span></code>: A string containing the name of a geometry field to use
for the <code class="docutils literal notranslate"><span class="pre">geometry</span></code> key of the GeoJSON feature. This is only needed when you
have a model with more than one geometry field and you don't want to use the
first defined geometry field (by default, the first geometry field is picked).</li>
<li><code class="docutils literal notranslate"><span class="pre">srid</span></code>: The SRID to use for the <code class="docutils literal notranslate"><span class="pre">geometry</span></code> content. Defaults to 4326
(WGS 84).</li>
</ul>
<p>The <a class="reference internal" href="../../../topics/serialization.html#subset-of-fields"><span class="std std-ref">fields</span></a> option can be used to limit fields that
will be present in the <code class="docutils literal notranslate"><span class="pre">properties</span></code> key, as it works with all other
serializers.</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.serializers</span> <span class="kn">import</span> <span class="n">serialize</span>
<span class="kn">from</span> <span class="nn">my_app.models</span> <span class="kn">import</span> <span class="n">City</span>

<span class="n">serialize</span><span class="p">(</span><span class="s1">&#39;geojson&#39;</span><span class="p">,</span> <span class="n">City</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">(),</span>
          <span class="n">geometry_field</span><span class="o">=</span><span class="s1">&#39;point&#39;</span><span class="p">,</span>
          <span class="n">fields</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,))</span>
</pre></div>
</div>
<p>将会输出：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;FeatureCollection&#39;</span><span class="p">,</span>
  <span class="s1">&#39;crs&#39;</span><span class="p">:</span> <span class="p">{</span>
    <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span>
    <span class="s1">&#39;properties&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;EPSG:4326&#39;</span><span class="p">}</span>
  <span class="p">},</span>
  <span class="s1">&#39;features&#39;</span><span class="p">:</span> <span class="p">[</span>
    <span class="p">{</span>
      <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;Feature&#39;</span><span class="p">,</span>
      <span class="s1">&#39;geometry&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;Point&#39;</span><span class="p">,</span>
        <span class="s1">&#39;coordinates&#39;</span><span class="p">:</span> <span class="p">[</span><span class="o">-</span><span class="mf">87.650175</span><span class="p">,</span> <span class="mf">41.850385</span><span class="p">]</span>
      <span class="p">},</span>
      <span class="s1">&#39;properties&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Chicago&#39;</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">]</span>
<span class="p">}</span>
</pre></div>
</div>
<p>When the <code class="docutils literal notranslate"><span class="pre">fields</span></code> parameter is not specified, the <code class="docutils literal notranslate"><span class="pre">geojson</span></code> serializer adds
a <code class="docutils literal notranslate"><span class="pre">pk</span></code> key to the <code class="docutils literal notranslate"><span class="pre">properties</span></code> dictionary with the primary key of the
object as the value.</p>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="ogrinspect.html"
                        title="上一章">OGR Inspection</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="commands.html"
                        title="下一章">GeoDjango Management Commands</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/gis/serializers.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="ogrinspect.html" title="OGR Inspection">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="commands.html" title="GeoDjango Management Commands">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>